package com.wangtianxiang.sxp.dbbase.config.processor;

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;

import java.util.Map;

/**
 * bean 处理适配器
 * 
 * @author lgyu4
 *
 */
public class SxpInstantiationAwareBeanPostProcessorAdapter extends InstantiationAwareBeanPostProcessorAdapter {

	private Map<String, AnnotatedBeanDefinition> customBeansBeanDefinition;
	private BeanDefinitionRegistry registry;

	
	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
			throws BeansException {
		if (null == customBeansBeanDefinition || !customBeansBeanDefinition.containsKey(beanName)) {
			return super.postProcessProperties(pvs, bean, beanName);
		}
		//mybatis mapper
		if (bean instanceof MapperScannerConfigurer) {
			AnnotatedBeanDefinition definition = customBeansBeanDefinition.get(beanName);
			MapperScannerConfigurer configurer = (MapperScannerConfigurer)bean;
			configurer.setSqlSessionFactoryBeanName(definition.getAttribute("sqlSessionFactoryBeanName")+"");
			configurer.setBasePackage(definition.getAttribute("basePackage")+"");
			configurer.postProcessBeanDefinitionRegistry(registry);
		}
		return pvs;
	}

	public void setCustomBeansBeanDefinition(Map<String, AnnotatedBeanDefinition> customBeansBeanDefinition) {
		this.customBeansBeanDefinition = customBeansBeanDefinition;
	}

	/**
	 * @param registry the registry to set
	 */
	public void setRegistry(BeanDefinitionRegistry registry) {
		this.registry = registry;
	}
	
	

}
